<?php defined('SYSPATH') or die('No direct script access.');

class Model_Color
{  
    const BLACK_COLOR       = 1;
    const WHITE_COLOR       = 2;
    const BLUE_COLOR        = 3;
    const GRAY_COLOR        = 4;
    const RED_COLOR         = 5;
    const DARK_BLUE_COLOR   = 6;
    const PINK_COLOR        = 7;
    const AZURE_COLOR       = 8;
    const BEIGE_COLOR       = 9;  
    const BROWN_COLOR       = 10;
    const GREEN_COLOR       = 11;
    const YELLOW_COLOR      = 12;
    const ORANGE_COLOR      = 13;
    const MILK_COLOR        = 14;
    const PURPLE_COLOR      = 15;
    const LILAC_COLOR       = 16; 
    
    public static $colors = array(
        'chernyiy'      => self::BLACK_COLOR,
        'belyiy'        => self::WHITE_COLOR,
        'siniy'         => self::BLUE_COLOR,
        'seryiy'        => self::GRAY_COLOR,
        'krasnyiy'      => self::RED_COLOR,
        'temno-siniy'   => self::DARK_BLUE_COLOR,
        'rozovyiy'      => self::PINK_COLOR,
        'goluboy'       => self::AZURE_COLOR,
        'bejevyiy'      => self::BEIGE_COLOR,
        'korichnevyiy'  => self::BROWN_COLOR,
        'zelenyiy'      => self::GREEN_COLOR,
        'jeltyiy'       => self::YELLOW_COLOR,
        'oranjevyiy'    => self::ORANGE_COLOR,
        'molochnyiy'    => self::MILK_COLOR, 
        'fioletovyiy'   => self::PURPLE_COLOR,
        'sirenevyiy'    => self::LILAC_COLOR,
    );
    
    public static $color_options = array(       
        self::BLUE_COLOR => array(
            'name'        => 'синий',
            'single_form' => 'синего цвета',    // женская оджеда синего цвета, одежду синего цвета
            'plural_form' => 'синие',           // синие брюки
        ), 
        self::RED_COLOR => array(
            'name'        => 'красный',
            'single_form' => 'красного цвета',
            'plural_form' => 'красные',
        ), 
        self::DARK_BLUE_COLOR => array(
            'name'        => 'темно-синий',
            'single_form' => 'темно-синего цвета',
            'plural_form' => 'темно-синие',
        ),
        self::PINK_COLOR => array(
            'name'        => 'розовый',
            'single_form' => 'розового цвета',
            'plural_form' => 'розовые',
        ),
        self::AZURE_COLOR => array(
            'name'        => 'голубой',
            'single_form' => 'голубого цвета',
            'plural_form' => 'голубые',
        ), 
        self::BEIGE_COLOR => array(
            'name'        => 'бежевый',
            'single_form' => 'бежевого цвета',
            'plural_form' => 'бежевые',
        ),
        self::BROWN_COLOR => array(
            'name'        => 'коричневый',
            'single_form' => 'коричневого цвета',
            'plural_form' => 'коричневые',
        ), 
        self::GREEN_COLOR => array(
            'name'        => 'зеленый',
            'single_form' => 'зеленого цвета',
            'plural_form' => 'зеленые',
        ),
        self::YELLOW_COLOR => array(
            'name'        => 'желтый',
            'single_form' => 'желтого цвета',
            'plural_form' => 'желтые',
        ),
        self::ORANGE_COLOR => array(
            'name'        => 'оранжевый',
            'single_form' => 'оранжевого цвета',
            'plural_form' => 'оранжевые',
        ),
        self::MILK_COLOR => array(
            'name'        => 'молочный',
            'single_form' => 'молочного цвета',
            'plural_form' => 'молочные',
        ),
        self::PURPLE_COLOR => array(
            'name'        => 'фиолетовый',
            'single_form' => 'фиолетового цвета',
            'plural_form' => 'фиолетовые',
        ),
        self::LILAC_COLOR => array(
            'name'        => 'сиреневый',
            'single_form' => 'сиреневого цвета',
            'plural_form' => 'сиреневые',
        ),
        self::BLACK_COLOR => array(
            'name'        => 'черный',
            'single_form' => 'черного цвета',
            'plural_form' => 'черные',
        ), 
        self::WHITE_COLOR => array(
            'name'        => 'белый',
            'single_form' => 'белого цвета',
            'plural_form' => 'белые',
        ),
        self::GRAY_COLOR => array(
            'name'        => 'серый',
            'single_form' => 'серого цвета',
            'plural_form' => 'серые',
        ), 
    );
    
    protected static function _compare($a, $b)
    {
        return $a['count'] < $b['count'];
    }
    
    public static function get_colors(Model_Category $category, $brand = null, $composition = null, $season = null, $shop = null, $price = null)
    {
        $ids = array(); 
        if ($category->childs) {
            $ids = explode(',', $category->childs);    
        }
        $ids[] = $category->id;
        
        $brand_id = $brand;
        if (is_array($brand)) {
            $brand_id = $brand['id'];   
        }
        
        $composition_id = $composition;
        if (is_array($composition)) {
            $composition_id = $composition['id'];    
        }
        
        $season_id = $season;
        if (is_array($season)) {
            $season_id = $season['id'];    
        }
        
        $shop_id = $shop;
        if (is_array($shop)) {
            $shop_id = $shop['id'];   
        }
        
        $from_price = $to_price = null;
        if (isset($price['from']) && $price['from']) {
            $from_price = $price['from'];    
        }
        if (isset($price['to']) && $price['to']) {
            $to_price = $price['to'];    
        }
        
        if ($colors = Cache::instance()->get('category_colors_'.$category->id.'_'.$brand_id.'_'.$composition_id.'_'.$season_id.'_'.$shop_id.'_'.$from_price.'_'.$to_price)) {
            return $colors;
        }
        
        $stat = DB::select('color_id', array(DB::expr('COUNT( * )'), 'count'))
                    ->from('product_colors')
                    ->join('products', 'LEFT')
                    ->on('products.id', '=', 'product_colors.product_id')
                    ->where('category_id', 'IN', $ids)
                    ->where('available', '=', 1);
                    
        if ($brand_id) {
            $stat->where('brand_id', '=', $brand_id); 
        }          
        
        if ($composition_id) {
            $stat->join('product_compositions', 'LEFT')
                  ->on('product_compositions.product_id', '=', 'products.id')
                  ->where('product_compositions.composition_id', '=', $composition_id);   
        }
        
        if ($season_id) {
            $stat->join('product_seasons', 'LEFT')
                  ->on('product_seasons.product_id', '=', 'products.id')
                  ->where('product_seasons.season_id', '=', $season_id);   
        }
        
        if ($shop_id) {
            $stat->where('shop_id', '=', $shop_id);    
        }
        
        if ($from_price) {
            $stat->where('price', '>=', $from_price);    
        }
        if ($to_price) {
            $stat->where('price', '<=', $to_price); 
        }
                    
        $stat = $stat
                    ->group_by('color_id')
                    ->order_by('count', 'DESC')
                    ->execute()
                    ->as_array();
        
        $colors = array();
        $all_colors = self::get_all_colors();
        foreach ($stat as $color) {
            $colors[$color['color_id']] = $all_colors[$color['color_id']];
            $colors[$color['color_id']]['count'] = $color['count'];            
        } 
        usort($colors, array(__CLASS__, '_compare'));
        
        Cache::instance()->set('category_colors_'.$category->id.'_'.$brand_id.'_'.$composition_id.'_'.$season_id.'_'.$shop_id.'_'.$from_price.'_'.$to_price, $colors);
        return $colors;       
    }
    
    public static function get_all_colors()
    {
        $colors = array();
        foreach (self::$colors as $url => $value) {
            $colors[$value] = self::$color_options[$value];
            $colors[$value]['url'] = $url;
            $colors[$value]['id']  = $value;        
        } 
        return $colors;   
    }
    
    public static function get_color_by_url($url)
    {
        $colors = self::get_all_colors();
        foreach ($colors as $color) {
            if ($color['url'] == $url) {
                return $color;    
            }    
        }
        return null;
    }
}